package com.ysb.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * @author husc
 * @date 2021/3/16
 */
@SuppressWarnings("ConstantConditions")
public class PathUtils {
    private static final String UTF_8 = "UTF-8";
    public static final String HMM_START_JSON_PATH = PathUtils.class.getClassLoader().getResource("hmm/hmm_start.json").getPath();
    public static final String HMM_PY2HZ_JSON_PATH = PathUtils.class.getClassLoader().getResource("hmm/hmm_py2hz.json").getPath();
    public static final String HMM_EMISSION_JSON_PATH = PathUtils.class.getClassLoader().getResource("hmm/hmm_emission.json").getPath();
    public static final String HMM_TRANSITION_JSON_PATH = PathUtils.class.getClassLoader().getResource("hmm/hmm_transition.json").getPath();

    //读取json文件
    public static JSONObject readJsonFile(String fileName) {
        JSONObject jsonObject = null;
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), UTF_8);
            int ch = 0;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            String jsonString = sb.toString();
            jsonObject = JSON.parseObject(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
